package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.naming.spi.DirStateFactory.Result;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;

import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Delete extends JInternalFrame {
	Connection conn;
	DatabaseTest main;
	
	private JLabel label;
	private JTextField serial;
	private JLabel label_1;
	private JTextField code;
	private JLabel label_2;
	private JTextField ea;
	private JButton button;
	private JPanel panel;
	private JLabel status;
	private JButton button_1;

	/**
	 * Create the frame.
	 */
	public Delete(Connection c, DatabaseTest m) {
		super("삭제", true, true, true, false);
		setBounds(100, 100, 450, 300);
		this.conn = c;
		this.main = m;
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getSerial());
		getContentPane().add(getLabel_1());
		getContentPane().add(getCode());
		getContentPane().add(getLabel_2());
		getContentPane().add(getEa());
		getContentPane().add(getButton());
		getContentPane().add(getPanel());
		getContentPane().add(getButton_1());
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("serial");
			label.setBounds(12, 13, 57, 15);
		}
		return label;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(81, 10, 116, 21);
		}
		return serial;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("code");
			label_1.setBounds(12, 41, 57, 15);
		}
		return label_1;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(81, 38, 116, 21);
		}
		return code;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("ea");
			label_2.setBounds(12, 69, 57, 15);
		}
		return label_2;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(81, 66, 116, 21);
		}
		return ea;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("삭제");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s = Integer.parseInt(serial.getText());
					String c = code.getText();
					String ee = ea.getText();
					
					try {
						String sql = "DELETE FROM PTBL WHERE SERIAL = ?";
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s);
						pstmt.executeUpdate();
						status.setText("삭제되었습니다.");
						code.setText("");
						ea.setText("");
						serial.requestFocus();
						pstmt.close();
					} catch (Exception e2) {
						// TODO: handle exception
					}
				}
			});
			button.setBounds(81, 97, 97, 23);
		}
		return button;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(SystemColor.info);
			panel.setBounds(12, 143, 294, 21);
			panel.add(getStatus());
		}
		return panel;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("검색할 코드를 입력하세요.");
		}
		return status;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("검색");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s = Integer.parseInt(serial.getText());
					String c = code.getText();
					String ee = ea.getText();
					
					try {
						String sql = "SELECT * FROM PTBL WHERE SERIAL = ?";
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s);
						ResultSet rs = pstmt.executeQuery();
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText(""+rs.getInt("ea"));
							status.setText("검색되었습니다.");
						}else{
							status.setText("삭제 할 데이터가 없습니다.");
						}
					} catch (Exception e2) {
						// TODO: handle exception
					}
				}
			});
			button_1.setBounds(209, 9, 70, 23);
		}
		return button_1;
	}
}
